package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.khyr.dao.CareOrderItemDao;
import com.khyr.dao.HisCareHistoryDao;
import com.khyr.dao.HisPatientDao;
import com.khyr.dao.HisPatientFileDao;
import com.khyr.entity.*;

import com.khyr.feign.ChargeManagementFeign;
import com.khyr.feign.DrugStatisticsFeign;

import com.khyr.service.PatientLibraryService;
import com.khyr.vo.GetPatientAllMessageByPatientIdVo;
import com.khyr.vo.PatientLibrarySearchVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：Zhang
 * @date ：Created in 2025/3/12 15:43
 * @description：患者库
 * @modified By：
 * @version: 1.0
 */
@Service
public class PatientLibraryServiceImpl implements PatientLibraryService {
    @Autowired
    private HisPatientDao hisPatientDao;
    @Autowired
    private HisPatientFileDao hisPatientFileDao;
    @Autowired
    private HisCareHistoryDao hisCareHistoryDao;
    @Autowired
    private ChargeManagementFeign chargeManagementFeign;
    @Autowired
    private CareOrderItemDao careOrderItemDao;
    @Autowired
    private DrugStatisticsFeign drugStatisticsFeign;
    /**
     * @create by: Zr
     * @description: 用于查询患者库的表格中的信息，可模糊查询,可分页
     * @create time: 2025/3/12 16:41
     * @param patientLibrarySearchVo
     * @return com.khyr.vo.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.khyr.entity.HisPatient>>
     */
    @Override
    public R<Page<HisPatient>> patientLibrarySearch(PatientLibrarySearchVo patientLibrarySearchVo) {
        QueryWrapper<HisPatient> queryWrapper = new QueryWrapper<>();
        // 添加查询条件
        if (patientLibrarySearchVo.getName() != null) {
            queryWrapper.like("name", patientLibrarySearchVo.getName());
        }
        if (patientLibrarySearchVo.getIdCard() != null) {
            queryWrapper.like("id_card", patientLibrarySearchVo.getIdCard());
        }
        if (patientLibrarySearchVo.getPhone() != null) {
            queryWrapper.like("phone", patientLibrarySearchVo.getPhone());
        }
        //开启分页
        Page<HisPatient> page = new Page<>(patientLibrarySearchVo.getPageNum(), patientLibrarySearchVo.getPageSize());
        Page<HisPatient> p = hisPatientDao.selectPage(page, queryWrapper);
        if (p != null) {
            return new R<>(200, "查询成功", p);
        }else {
            return new R<>(500, "查询失败", null);
        }
    }
    /**
     * @create by: Zr
     * @description: 根据患者id查询患者信息
     * @create time: 2025/3/12 16:42
     * @param patientId
     * @return com.khyr.vo.R<com.khyr.entity.HisPatient>
     */
    @Override
    public R<HisPatient> getPatientById(String patientId) {
        // 添加查询条件
        QueryWrapper<HisPatient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", patientId);
        if (hisPatientDao.selectOne(queryWrapper) != null) {
            return new R<>(200, "查询成功", hisPatientDao.selectOne(queryWrapper));
        }else {
            return new R<>(500, "查询失败", null);
        }
    }
    /**
     * @create by: Zr
     * @description: 根据id获取患者档案信息
     * @create time: 2025/3/12 16:59
     * @param patientId
     * @return com.khyr.vo.R<com.khyr.entity.HisPatientFile>
     */
    @Override
    public R<HisPatientFile> getPatientFileById(String patientId) {
        QueryWrapper<HisPatientFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", patientId);
        if (hisPatientFileDao.selectOne(queryWrapper) != null) {
            return new R<>(200, "查询成功", hisPatientFileDao.selectOne(queryWrapper));
        }else {
            return new R<>(500, "查询失败", null);
        }
    }
    /**
     * @create by: Zr
     * @description: 通过患者id查询病例及处方信息
     * @create time: 2025/3/12 20:39
     * @param patientId
     * @return com.khyr.vo.R<java.util.List<com.khyr.entity.HisCareHistory>>
     */
    @Override
    public R<List<HisCareHistory>> getPatientAllMessageByPatientId(String patientId) {
        QueryWrapper<HisCareHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", patientId);
        // 获得患者的病例信息
        List<HisCareHistory> hisCareHistories = hisCareHistoryDao.selectList(queryWrapper);
        if (hisCareHistories != null) {
            for(HisCareHistory hisCareHistory : hisCareHistories){
                // 根据获得的患者的病例信息获得该患者的病例id,即ch_id
                // 调用了services-chargeManagement服务中的/charge/Registration/getCareOrderByRegId/{regId}对应的getCareOrderByRegId方法，获得了用户的处方信息
                R<List<CareOrder>> careOrderByRegId = chargeManagementFeign.getCareOrderByRegId(hisCareHistory.getChId());
                if (careOrderByRegId.getCode() == 200) {
                        // 通过获取的用户的处方id,即co_id,查询开诊细表，获得用户所用的药品及检查项目

                        for(CareOrder careOrder : careOrderByRegId.getData()){
                            QueryWrapper<CareOrderItem> queryWrapper1 = new QueryWrapper<>();
                            queryWrapper1.eq("co_id", careOrder.getCoId());
                            List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(queryWrapper1);
                            careOrder.setCareOrderItems(careOrderItems);
                        }
                    hisCareHistory.setCareOrders(careOrderByRegId.getData());
                }
            }

            return new R<>(200, "查询成功", hisCareHistories);
        }else {
            return new R<>(500, "查询失败", null);
        }

    }
    /**
     * @create by: Zr
     * @description: 根据患者身份证号查询患者信息
     * @create time: 2025/3/13 16:03
     * @param idCard
     * @return com.khyr.vo.R<com.khyr.entity.HisPatient>
     */
    @Override
    public R<HisPatient> getPatientInfoByIdCar(String idCard) {
        QueryWrapper<HisPatient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", idCard);
        if (hisPatientDao.selectOne(queryWrapper) != null) {
            return new R<>(200, "查询成功", hisPatientDao.selectOne(queryWrapper));
        }else {
            return new R<>(500, "查询失败", null);
        }
    }
}
